package cn.b.sky.mes.em.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.b.sky.mes.em.dao.RepairCategoryDao;
import cn.b.sky.mes.em.model.RepairCategory;
import cn.b.sky.mes.em.model.condition.RepairCategoryCondition;
import cn.b.sky.mes.em.service.RepairCategoryService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;

@Service("repairCategoryService")
public class RepairCategoryServiceImpl implements RepairCategoryService {

  private static final Logger LOG = LoggerFactory.getLogger(RepairCategoryServiceImpl.class);

  @Resource
  RepairCategoryDao repairCategoryDao;

  @Override
  public List<RepairCategory> selectRepairCategoryListByCondition(RepairCategoryCondition condition) {
    if (condition == null) {
      condition = new RepairCategoryCondition();
    }
    if (StringUtils.isBlank(condition.getOrderField())) {
      condition.setOrderField("CreateDateTime");
    }
    return repairCategoryDao.selectRepairCategoryListByCondition(condition);
  }

  @Override
  public int selectRepairCategoryCount(RepairCategoryCondition condition) {
    if (condition == null) {
      condition = new RepairCategoryCondition();
    }
    return repairCategoryDao.selectRepairCategoryCount(condition);
  }

  @Override
  public void updateRepairCategory(RepairCategory repairCategory) {
    if (repairCategory != null && !StringUtils.isBlank(repairCategory.getCategoryCode())) {
      repairCategoryDao.updateRepairCategory(repairCategory);
    } else {
      return;
    }

  }

  @Override
  public void insertRepairCategory(RepairCategory repairCategory) {
    if (repairCategory != null && !StringUtils.isBlank(repairCategory.getCategoryCode())) {
      repairCategoryDao.insertRepairCategory(repairCategory);
    } else {
      return;
    }

  }

  @Override
  public BatchJobReport deleteRepairCategory(String categoryCode, String siteCode) {
    if (StringUtils.isBlank(siteCode) || StringUtils.isBlank(categoryCode)) {
      return new BatchJobReport("", LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
          SecurityContextUtil.getLanguageCode(), "parameterError.M"), false);
    } else {
      String message = categoryCode;
      String cause = "";
      boolean success = true;
      try {
        repairCategoryDao.deleteRepairCategory(categoryCode, siteCode);
      } catch (Exception e) {
        LOG.error("{}", e);
        success = false;
        String errorMsg = e.getMessage();
        if (DatabaseUtils.isConstraintsError(e.getMessage())) {
          //外键删除已存在提示
          errorMsg = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
              SecurityContextUtil.getLanguageCode(), "dataBaseOperationPkFail.M")
              + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
                  SecurityContextUtil.getLanguageCode(), "notDelete.M");
        }
        cause = errorMsg;
      }
      return new BatchJobReport(message, cause, success);
    }
  }

  @Override
  public RepairCategory getRepairCategory(String categoryCode, String siteCode) {
    if (StringUtils.isBlank(siteCode) || StringUtils.isBlank(categoryCode)) {
      return null;
    } else {
      return repairCategoryDao.getRepairCategory(categoryCode, siteCode);
    }
  }

  @Override
  public List<RepairCategory> selectAllRepairCategoryListByCondition(RepairCategoryCondition condition) {
    if (condition == null) {
      condition = new RepairCategoryCondition();
    }
    if (StringUtils.isBlank(condition.getOrderField())) {
      condition.setOrderField("CreateDateTime");
    }
    return repairCategoryDao.selectAllRepairCategoryListByCondition(condition);
  }

  @Override
  public List<RepairCategory> selectRepairCategoryForSuggest(RepairCategoryCondition condition) {
    return repairCategoryDao.selectRepairCategoryForSuggest(condition);
  }

}
